<?php
declare(strict_types=1);

namespace App\Http\Model\Flow;
use Illuminate\Support\Facades\DB;
use App\Http\Library\Excel;
use App\Http\ModelDao\Flow\ClassTransferDao;

class ClassTransferModel
{
/*数据集合*/
    private static $rows;
    /*数据长度*/
    private static $rowsCount = 0;
    /*未删除的数据*/
    const IS_DEL=["is_del","=",0];

    private static $fields = [
        'id',
        'push_time',
        //"城市名称",
        'transfer_id',
        'transfer_num',
        'sum_transfer_amt',
        'apply_date',
        'result',
        'pfs_desc'
    ];
    /*
     * @param array $params
     *
     * return array
     */
    public static function index($params = []):array{

            $limit =  (int)$params["pagesize"];
            $offset = ((int)$params["page"]-1)*$limit;
            $where = [];
            /*返回状态*/
            if(!isEmpty($params["result"])){
                $where[] = ["result","=",$params["result"]];
            }
            /*城市名称*/
            if(!isEmpty($params["city_name"])){
                $where[] = ["city_name","=",$params["city_name"]];
            }
            /*主体名称*/
            if(!isEmpty($params["ledger_subject_name"])){
                $where[] = ["ledger_subject_name","=",$params["ledger_subject_name"]];
            }
            /*汇总时间-区间*/
             if(!isEmpty($params["collect_ctime"]) && !isEmpty($params["collect_etime"])){
                $where[] = ["push_time",">=",dateFrom($params["collect_ctime"])];
                $where[] = ["push_time","<=",dateTo($params["collect_etime"])];
            }
             /*未删除的数据*/
            array_push($where,self::IS_DEL);
            //DB::connection()->enableQueryLog();#开启执行日志

            //download select
            //
            $fields = DB::raw('id,city_name,ledger_subject_name,transfer_id,transfer_num,sum_transfer_amt,apply_date,push_time,result,result_desc,pfs_desc');
            if($params["download"] == 'All'){
                /*获取全部数据*/
                self::$rows = DB::table("class_amt_transfer")->wherein('pfs_dw_id',$params['pfs_dw_id'])->select($fields)->where($where)->orderBy("id","desc")->get();
                self::$rowsCount = sizeof(self::$rows);
            }else{
                self::$rows = DB::table("class_amt_transfer")->wherein('pfs_dw_id',$params['pfs_dw_id'])->select($fields)->where($where)->orderBy("id","desc")->offset($offset)->limit($limit)->get();
                self::$rowsCount = DB::table("class_amt_transfer")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->count();
            }

            //print_r(DB::getQueryLog());   //获取查询语句、参数和执行时间

        self::download($params);

        return [
            'list' => self::$rows,
            'allRows' => self::$rowsCount,
        ];
    }
    /*
     * 数据下载
    */
    private static function download($params){
        //
        if(in_array($params["download"],['All','Current'])){
            Excel::$download = true;
            Excel::$rowWidth = [
                'A:A' => 12,
                'B:B' => 16,
                'C:C' => 22,
                'D:D' => 15,
                'E:E' => 11,
                'F:F' => 14,
                'G:G' => 19,
                'H:H' => 17,
                'I:I' => 14,
                'J:J' => 14
            ];
            $title = ['推送时间','城市','主体','划拨申请编号','划拨申请课程数','本次申请划拨总金额','申请日期','推送状态','推送描述','返回描述'];
            //$title = ['a','b','c','d'];
            Excel::$title = $title;

           //var_dump(ClassTransactionDetailDao::numberResult(self::$rows));exit();
            Excel::send(ClassTransferDao::numberResult(self::$rows),'班级划拨-'.date("Ymd-His").'.xlsx');
             exit();
        }
    }
}
